package com.gitee.wsl.compose.ext.text.span.painter

import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.AnnotatedString
import androidx.compose.ui.text.LinkAnnotation
import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.TextLayoutResult
import androidx.compose.ui.graphics.isUnspecified
import androidx.compose.ui.graphics.toArgb

abstract class ExtendedSpanPainter {
    /**
     * Can be used for removing any existing spans from [text] so that they can be drawn manually.
     */
    abstract fun decorate(
        span: SpanStyle,
        start: Int,
        end: Int,
        text: AnnotatedString,
        builder: AnnotatedString.Builder,
    ): SpanStyle

    /**
     * Can be used for removing any existing text link styles from [text] so that they can be drawn manually.
     */
    abstract fun decorate(
        linkAnnotation: LinkAnnotation,
        start: Int,
        end: Int,
        text: AnnotatedString,
        builder: AnnotatedString.Builder,
    ): LinkAnnotation

    abstract fun drawInstructionsFor(
        layoutResult: TextLayoutResult,
        color: Color? = null,
    ): SpanDrawInstructions
}


internal fun Color?.serialize(): String {
    return if (this == null || isUnspecified) "null" else "${toArgb()}"
}

internal fun String.deserializeToColor(): Color? {
    return if (this == "null") null else Color(this.toInt())
}

internal fun Color?.colorOrNull(): Color? {
    return if (this == null || isUnspecified) null else this
}